﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LoginForm
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
            txtPassword.PasswordChar = '*';
            if (chbNoteAccount.Checked) {
                NoteAccount();
            }
            
        }

        private bool IsvalidUser(string filename, User u)
        {
            List<User> list = SunnyIO.GetUserFromXML(filename);
            foreach (User user in list)
            {
                string name = user.Username;
                string pass = user.Password;
                if (name.Contains(u.Username)) {
                    if (pass.Contains(u.Password)) {
                        return true;
                    }
                }
            }
            return false;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void Login()
        {
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            string[] strArr = new string[] { username, password };
            if (Validation.IsValidTextBox(strArr))
            {
                User user = new User()
                {
                    Username = username,
                    Password = password
                };
                bool chk = IsvalidUser(FileName.user, user);
                if (chk)
                {
                    if (chbNoteAccount.Checked)
                    {
                        SunnyIO.SaveNoteAccount(user);
                    }
                    else
                    {
                        SunnyIO.SaveNoteAccount(null);
                    }
                    this.Hide();
                    Main form = new Main();
                    form.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Failure : Username or Password is not correct !", "Error Dialog", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Error: Username or Password must not be empty !", "Error Dialog", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void NoteAccount() {
            User u = SunnyIO.ReadNoteAccount();
            if (u != null)
            {
                txtUsername.Text = u.Username;
                txtPassword.Text = u.Password;
            }
            else {
                txtUsername.Text = "";
                txtPassword.Text = "";
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("Do you want to exit ?", "Exit Dialog", MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (result == DialogResult.Yes) {
                Application.Exit();
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void txtUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) {
                Login();                
            }
            if (e.KeyCode == Keys.Escape) {
                Application.Exit();
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Login();
            }
            if (e.KeyCode == Keys.Escape)
            {
                Application.Exit();
            }
        }

        

        
        
    }
}
